<script>
    $(document).ready(function () {
        $( "input:submit").button();
        $("#transactioncrdr").focus();
        $("#Transaction_transactionmasternarration").prop('disabled', true);
    });
//        $.ajax({
//          url: "index.php?r=transaction/initrow",
//          success: function(data){
//                            $.fn.yiiGridView.update("transaction-grid", {
//                                data: $(this).serialize()
//                            });                        
//          }
//        });

function myf(){
    
}
function ajaxSubmit(option){
    if($("#transactioncrdr").val() !='' && $("#acledger").val() !='' && $("#transactionamount").val()!=''){

    $.ajax({
        url: "<?php echo $this->createUrl('transaction/addrow'); ?>",
        type: "POST",
        dataType: "json",
        data: jQuery("#transaction-form").serialize(),
        success: function (data) {

            $.fn.yiiGridView.update("transaction-grid", {
                //data: $(obj).serialize()
            });
            if(option == 'clear'){
                $("#transactioncrdr").focus();
                $("#id").val("-1");
                $("#transactioncrdr").val("");
                $("#acledger").val("");
                $("#transactionamount").val("");
            }
            if(data.credit > 0 && data.debit > 0 && data.credit == data.debit){                
                $("#Transaction_transactionmasternarration").prop('disabled', false);                
                $("#Transaction_transactionmasternarration").focus();
            }
            else{
                $("#Transaction_transactionmasternarration").prop('disabled', true);
            }

//        if($('#id').val()>=0){
//            $('#transaction-grid_c0_' + $('#id').val()).attr('checked', true);
//        }

        return true;
        }
    });    
    }
}
</script>

<style>
    .grid-view table{
        border: none;
    }
</style>

<?php
//    Yii::app()->clientscript->scriptMap['jquery.js'] = false;
//    Yii::app()->clientscript->scriptMap['jquery-ui.min.js'] = false;

        //Yii::app()->clientscript->scriptMap['jquery.yiigridview.js'] = true;

?>


<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
	'id'=>'transaction-form',
	'enableAjaxValidation'=>true,
)); ?>

	<p class="note"><?php  echo Yii::t('application', 'Fields with <span class="required">*</span> are required.');?></p>

	<?php echo $form->errorSummary($model); ?>

	<div class="row"> 
                <?php echo $form->labelEx($model,'transactioncrdr'); ?>            
		<?php 
                    $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                        'name'=>'transactioncrdr',
                        'id'=>'transactioncrdr',
                        'theme' => 'bgscstpc',
                        'source'=>array('CR', 'DR'),
                            'htmlOptions' => array(
                         ),
                        'options'=>array(
                            'showAnim'=>'fold',
                            'change'=>'js:function(event, ui) {
                                ajaxSubmit(""); return true;
                             }'
                        ),
                    ));
                    ?>
		<?php echo $form->error($model,'transactioncrdr'); ?>
	</div>
        
	<div class="row"> 
                <?php echo $form->labelEx($model,'acledger'); ?>            
                <?php
                    $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
                        'name'=>'acledger',
                        'id'=>'acledger',
                        //'model' => $model, 
                        //'id' => 'Acledger_acgroupname',
                        'theme' => 'bgscstpc',
                        //'source' => $this->createUrl('transaction/autocompleteAcledger', array('type' => $type)),
                        'source'=>'js: function(request, response) {
                            $.ajax({
                                url: "'.$this->createUrl('transaction/autocompleteAcledger').'",
                                dataType: "json",
                                data: {
                                    term: request.term,
                                    type: "'.$type.'",
                                    transactioncrdr: $("#transactioncrdr").val()
                                },
                                success: function (data) {
                                        response(data);
                                }
                            })
                         }',
                        
                        // additional javascript options for the autocomplete plugin
                        'options'=>array(
                            'showAnim'=>'fold',
                            'change'=>'js:function(event, ui) {
                                ajaxSubmit(""); return true;
                             }'
                        ),
                        'htmlOptions' => array(
                        )
                    ));
                ?>
		<?php echo $form->error($model,'acledger'); ?>
	</div>

	<div class="row">
                <?php echo $form->labelEx($model,'transactionamount'); ?>            
		<?php 
                    echo CHTML::textField(
                            'transactionamount',
                            '',
                            $htmlOptions = array(
                                'rows'=>6, 
                                'cols'=>50,
                                'onchange'=>'
                                    ajaxSubmit("clear");                                    
                                 ',
                                    

//                                    $.ajax({
//                                      url: "index.php?r=transaction/addrow",
////                                      type:"POST",
//                                        data: {
//                                            id: $("#id").val()
//                                            transactioncrdr: $("#transactioncrdr").val()
//                                        },                                      
//                                      success: function( data ) {
//                                        alert(data);
////                                        $.fn.yiiGridView.update(\"transaction-grid\", {
////                                            data: $(this).serialize()
////                                        });
////                                        $(\"#transactioncrdr\").focus();
////                                        $(\"#id\").val(\"-1\");
////                                        $(\"#transactioncrdr\").val(\"\");
////                                        $(\"#acledger\").val(\"\");
////                                        $(\"#transactionamount\").val(\"\");
//
//                                        return true;
//                                      }
//                                    });'
                            )
                    ); 
                ?>
		<?php echo $form->error($model,'transactionamount'); ?>
	</div>
        
	<?php echo CHTML::hiddenField('id', '-1'); ?>
        
        
        
        <div id="gridcontainer">
            <?php 
            $columns = array(
		array(
                    'class'=>'CCheckBoxColumn',
                    'footerHtmlOptions' => array('style' => 'border: none;'),
		),                
                array(
                    'name' => 'id',
//                    'value' => 'CHtml::textField("transactiondetails[" . $data[\'id\'] . "][id]", $data[\'id\'], array(\'width\'=>20,\'size\'=>7,\'maxlength\'=>30,\'style\'=>\'text-align:right\'))',
                    'value'=>'$data[\'id\']', 
                    'type' => 'raw',
                    'footerHtmlOptions' => array('style' => 'border: none;'),
                    'htmlOptions' => array('style' => 'display:none;'),                    
                    'headerHtmlOptions' => array('style' => 'display:none'),
                    'footerHtmlOptions' => array('style' => 'display:none'),
                ),
                array(
                    'name' => 'openingbalancecrdr',      
                    'type' => 'raw',
                    'value'=>'$data[\'openingbalancecrdr\']', 
                    'htmlOptions' => array('style' => 'width:10%;'),
                    'headerHtmlOptions' => array('style' => 'width:10%;'),
                    'footerHtmlOptions' => array('style' => 'border: none;width:10%;'),
                ),
                array(
                    'name' => 'acledger',      
                    'type' => 'raw',
                    'value'=>'$data[\'acledger\']', 
                    'htmlOptions' => array('style' => 'width:60%;'),
                    'headerHtmlOptions' => array('style' => 'width:60%;'),
                    'footerHtmlOptions' => array('style' => 'border: none;width:60%;'),
                ),
                array(
                    'name' => 'debit',      
                    'type' => 'raw',
                    'value'=>'$data[\'debit\']', 
                    'footer'=> $this->getDebit(),
                    'htmlOptions' => array('style' => 'text-align:right;width:15%;'),
                    'headerHtmlOptions' => array('style' => 'width:15%;'),
                    'footerHtmlOptions' => array('style' => 'text-align:right;font-weight:bold;width:15%;border-width: 4px; border-style: double; border-right: none; border-left: none; border-top: none;'),
                ),
                array(
                    'name' => 'credit',      
                    'type' => 'text',
                    'value'=>'$data[\'credit\']', 
                    'footer'=> $this->getCredit(),
                    'htmlOptions' => array('style' => 'text-align:right;width:15%;'),
                    'headerHtmlOptions' => array('style' => 'width:15%;'),
                    'footerHtmlOptions' => array('style' => 'text-align:right;font-weight:bold;width:15%;border-width: 4px; border-style: double; border-right: none; border-left: none; border-top: none;'),
                ),
                array(
                    'name' => 'transactionamount',      
                    'type' => 'raw',
                    'value'=>'$data[\'transactionamount\']', 
                    'htmlOptions' => array('style' => 'display:none;'),                    
                    'headerHtmlOptions' => array('style' => 'display:none'),
                    'footerHtmlOptions' => array('style' => 'display:none'),
                ),
        //    echo $form->dropDownList($model,'openingbalancecrdr',array('CR'=>'CR', 'DR'=>'DR'));        
            );
            
            $wid = $this->widget('ext.eziiui.widgets.grid.CGridViewUI', array(
//            $wid = $this->widget('ext.EDataTables.EDataTables', array(
                'id' => 'transaction-grid',
                'dataProvider' => $dataProvider,
                'columns' => $columns,
                //'enablePagination' => false,
                'summaryText' => '',
        //        'filter' => $filtersForm,
                'selectionChanged'=>'function(id){onSelectionChange()}',
                'beforeAjaxUpdate'=>'function(id, data){
                        $("#transactioncrdr").focus();
                 }',
                'afterAjaxUpdate'=>'function(id, data){
                        if($("#id").val()>=0){
                            $("#transaction-grid_c0_" + $("#id").val()).attr("checked", true);
                            $("#transaction-grid_c0_" + $("#id").val()).parent().parent().attr("class", "selected ui-state-highlight");;
                        }
                        //alert("hii");
                    }'
            ));
            ?>            
        </div>
        
	<div class="row"> <!--transactionmasternarration -->
                <?php echo $form->labelEx($model,'transactionmasternarration'); ?>
		<?php //echo $form->textField($model,'transactionmasternarration',array('size'=>60,'maxlength'=>255)); ?>
		<?php echo $form->textArea($model,'transactionmasternarration',array('rows'=>6, 'cols'=>50)); ?>
		<?php echo $form->error($model,'transactionmasternarration'); ?>
	</div>
        
	<div class="row"> <!--idvouchertype -->
		<?php echo $form->hiddenField($model,'idvouchertype'); ?>
	</div>
        
	<div class="row"> <!--transactiondetails -->
                <?php //echo $form->labelEx($model,'transactiondetails'); ?>            
		<?php //echo $form->textArea($model,'transactiondetails',array('rows'=>6, 'cols'=>50)); ?>
		<?php //echo $form->error($model,'transactiondetails'); ?>
	</div>
        
        
<script type="text/javascript">
    function onSelectionChange()
    {
            var keys = $("#transaction-grid > div.keys > span");
            $("#id").val('-1');
            $("#transactioncrdr").val('');
            $("#acledger").val('');
            $("#transactionamount").val('');

            $("#transaction-grid > table > tbody > tr").each(function(i)
            {
                    if($(this).hasClass("selected"))
                    {
                        $("#id").val($(this).children(":nth-child(2)").text());
                        $("#transactioncrdr").val($(this).children(":nth-child(3)").text());
                        $("#acledger").val($(this).children(":nth-child(4)").text());
                        $("#transactionamount").val($(this).children(":nth-child(7)").text());
                        //alert($("#id").val());

                        //alert($(this).children(":nth-child(2)").text());
    //                        $("#Pacjenci_patientNAME").val($(this).children(":nth-child(2)").text());
    //                        $("#Pacjenci_patientID").val(keys.eq(i).text());
                    }
                    else{
                        //$("#id").val('-1');                        
                    }
            });
            $("#transactioncrdr").focus();
    }    
function userClicks(target_id) {
        alert($.fn.yiiGridView.getSelection(target_id));
}
</script>        
        
	<div class="row buttons">
        <?php
        echo CHtml::ajaxSubmitButton(
                "Add SB", 
                array('transaction/addrow'), // the URL for the AJAX request. If empty, it is assumed to be the current URL.
                array(
                    'success' => 'function(data){
                            $.fn.yiiGridView.update("transaction-grid", {
                                data: $(this).serialize()
                            });
                            $("#transactioncrdr").focus();
                            $("#id").val("-1");
                            $("#transactioncrdr").val("");
                            $("#acledger").val("");
                            $("#transactionamount").val("");
                            
                            return true;
                        }'
        ) 
        );       
        
        ?>
            
		<?php //echo CHtml::submitButton($model->isNewRecord ? Yii::t('application', 'Create') : Yii::t('application', 'Save')); ?>
	</div>
        
        <?php
        ?>
        
<?php $this->endWidget(); ?>

</div><!-- form -->

<?php
$this->widget('ext.ajaxform.JAjaxForm', array(
    'formId' => 'transaction-form',
    'options' => array(
        'dataType' => 'json',
        'success' => 'js:function(data,statusText) { 
                    if(data.status == "Success"){
                        $.fn.yiiGridView.update("transaction-grid", {
                              data: $(this).serialize()
                        });
                        $("#dialog").dialog("close").destroy();
                        return false;
                    }
                    else{
                        $("#dialog-warning-msg").html("' . Yii::t('application', 'An error occurred while the form was being submitted.<br/>Please check your form data.') . '" + data.status); 
                        $("#dialog-warning").dialog("open"); 
                        return false;
                    }
        }',
    ),
));
?>
